<?php

if(!isSet($_GET["tabela"]) || !isSet($_GET["wiersze"])){
  die("error\nNieprawidowe wywoanie skryptu.");
}

$tabela = $_GET["tabela"];
$wiersze = $_GET["wiersze"];

if($tabela == ""){
  die("error\nBrak nazwy tabeli.");
}

if(!is_numeric($wiersze)){
  die("error\nNieprawidowa liczba wierszy.");
}

if (!$db_lnk = sqlite_open("./baza.sqlite", 0666, $msg)){
  die("error\nBrak poczenia z baz danych.");
}

if($wiersze <= 0){
  $limit = "";
}
else{
  $limit = "LIMIT $wiersze";
}

$query = "SELECT * FROM '$tabela' " . $limit;

if(!$result = @sqlite_query($db_lnk, $query, $SQLITE_ASSOC)){
  die("Nie ma takiej tabeli w bazie.");
}

if(!$row = sqlite_fetch_array($result)){
  die("Tabela nie zawiera ona danych.");
}
?>

<table border="0">
<tr style="font-weight:bold;">

<?php
foreach($row as $key => $val){
  echo "<td>$key</td>";
}
?>

</tr>

<?php
do{
  echo "<tr>";
  foreach($row as $val){
    echo "<td>$val</td>";
  }
  echo "</tr>";
}
while($row = sqlite_fetch_array($result));
?>

</tr>
</table>